///////////////////////////////////////////////////////////////////////////////
///// Assignment 3: CPU Scheduler
///// By: Brady Robichaud (robicb82) & Patrick Filtz (filtzp43)
///// Date: 4/19/2013
///// File: Window.java
///// Description: Creates a GUI for the CPU Scheduler, prints out queues
///////////////////////////////////////////////////////////////////////////////
package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import round2.Main;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.ScrollPane;
import javax.swing.JList;
import javax.swing.JScrollPane;
import java.awt.Font;

public class Window {

	private JFrame frame;
	private JButton btnAddProcess;
	JPanel pnlMain;
	JButton btnRun;
	JButton btnClear;
	JTextArea txtProcs;
	JScrollPane scrollPane;
	private JScrollPane scrollPane2;
	private JTextArea txtChart;
	private boolean ready = false;
	private boolean added = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Window window = new Window();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Window() {
		initialize();
	}
	
	public void addProc(){
		txtProcs.append(Main.addProc() + "\n");
		added = true;
		btnRun.setEnabled(true);
	}
	
	public void clear(){
		txtProcs.setText("");
		txtChart.setText("");
		Main.clearAll();
	}
	
	public void run(){
		if(added){
			txtChart.setText("");
			txtChart.append(Main.run());
			btnRun.setEnabled(false);
			added = false;
		}
	}
	

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 1024, 768);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pnlMain = new JPanel();
		frame.getContentPane().add(pnlMain, BorderLayout.CENTER);
		pnlMain.setLayout(null);
		
		btnAddProcess = new JButton("Add Process");
		btnAddProcess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addProc();
			}
		});
		btnAddProcess.setBounds(32, 22, 144, 45);
		pnlMain.add(btnAddProcess);
		
		btnRun = new JButton("Run");
		btnRun.setEnabled(false);
		btnRun.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				run();
			}
		});
		btnRun.setBounds(32, 104, 144, 45);
		pnlMain.add(btnRun);
		
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clear();
			}
		});
		btnClear.setBounds(866, 665, 128, 45);
		pnlMain.add(btnClear);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(237, 22, 757, 127);
		pnlMain.add(scrollPane);
		
		txtProcs = new JTextArea();
		scrollPane.setViewportView(txtProcs);
		
		scrollPane2 = new JScrollPane();
		scrollPane2.setBounds(32, 162, 962, 490);
		pnlMain.add(scrollPane2);
		
		txtChart = new JTextArea();
		txtChart.setFont(new Font("DialogInput", Font.PLAIN, 13));
		scrollPane2.setViewportView(txtChart);
	}
}
